package com.longcai.service.impl.goods;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homegood.ClyjGoodsSpec;
import com.longcai.entity.homegood.ClyjGoodsVo;
import com.longcai.entity.homepage.ClyjGoodsType;
import com.longcai.entity.homepage.GoodsClass;
import com.longcai.mapper.goods.ClyjGoodsHomepageMapper;
import com.longcai.service.goods.ClyjGoodsHomepageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.util.Page;
import com.longcai.util.SolrConnect;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 首页商品表 服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-03-22
 */
@Service
public class ClyjGoodsHomepageServiceImpl extends ServiceImpl<ClyjGoodsHomepageMapper, ClyjGoodsHomepage> implements ClyjGoodsHomepageService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsHomepageMapper clyjGoodsHomepageMapper;
	@Override
	public List<GoodsClass> listByBrandId(String goodsId) {
		return clyjGoodsHomepageMapper.listByBrandId(goodsId);
	}
	//删除类型
	@Override
	public void deleteType(ClyjGoodsVo goodsClas2s) {
		clyjGoodsHomepageMapper.deleteType(goodsClas2s.getGoodsId());
	}
	//新增类型
	@Override
	public void insertType(ClyjGoodsVo goodsClas2s) {
		List<String> typeIds = goodsClas2s.getTypeIds();
		if(typeIds==null || typeIds.size()==0)
			return;
		for (String s : typeIds) {
			Map map = new HashMap();
			map.put("id", UUID.randomUUID().toString());
			map.put("goodsId", goodsClas2s.getGoodsId());
			map.put("typeId", s);
			clyjGoodsHomepageMapper.insertType(map);
		}
	}
	//删除规格
	@Override
	public void deleteSpec(ClyjGoodsVo goodsClas2s) {
		clyjGoodsHomepageMapper.deleteSpec(goodsClas2s.getGoodsId());
	}
	//新增规格
	@Override
	public void insertSpec(ClyjGoodsVo goodsClas2s) {
		List<String> specDetail = goodsClas2s.getSpecDetail();
		List<String> specSort = goodsClas2s.getSpecSort();
		List<String> specNum = goodsClas2s.getSpecNum();
		List<BigDecimal> specPriceOld = goodsClas2s.getSpecPriceOld();
		List<BigDecimal> specPriceNew = goodsClas2s.getSpecPriceNew();
		List<BigDecimal> specYj = goodsClas2s.getSpecYj();
		List<String> specKcjg = goodsClas2s.getSpecKcjg();
		int num=0;
		if(specDetail==null || specDetail.size()==0)
			return;
		for (int i=0 ; i<specDetail.size();i++){
			Map map = new HashMap();
			map.put("id", UUID.randomUUID().toString());
			if(goodsClas2s.getGoodsId()!=null && !goodsClas2s.getGoodsId().equals(""))
				map.put("goodsId", goodsClas2s.getGoodsId());
			if(specDetail.get(i)!=null && !specDetail.get(i).equals(""))
				map.put("detail", specDetail.get(i));
			if(specSort.get(i)!=null && !specSort.get(i).equals(""))
				map.put("sort",specSort.get(i));
			if(specNum.get(i)!=null && !specNum.get(i).equals(""))
				map.put("num", specNum.get(i));
			if(specPriceOld.get(i)!=null && !specPriceOld.get(i).equals(""))
				map.put("specPriceOld", specPriceOld.get(i));
			if(specPriceNew.get(i)!=null && !specPriceNew.get(i).equals(""))
				map.put("specPriceNew", specPriceNew.get(i));
			if(specYj.get(i)!=null && !specYj.get(i).equals(""))
				map.put("specYj", specYj.get(i));
			if(specKcjg.get(i)!=null && !specKcjg.get(i).equals(""))
				map.put("specKcjg", specKcjg.get(i));
			clyjGoodsHomepageMapper.insertSpec(map);
			num = num+Integer.valueOf(specNum.get(i));
		}
		//修改商品库存
		ClyjGoodsHomepage g = new ClyjGoodsHomepage();
		g.setGoodsId(goodsClas2s.getGoodsId());
		g.setGoodsStock(num);
		clyjGoodsHomepageMapper.updateById(g);
	}
	@Override
	public List<ClyjGoodsSpec> getSpecByGoodsId(String goodsId) {
		// TODO Auto-generated method stub
		return clyjGoodsHomepageMapper.getSpecByGoodsId(goodsId);
	}
	@Override
	public void deletePics(ClyjGoodsVo goodsClas2s) {
		clyjGoodsHomepageMapper.deletePics(goodsClas2s.getGoodsId());
	}
	@Override
	public ApiResult getGoodsByType(String string) {
		List<ClyjGoodsType> a = clyjGoodsHomepageMapper.selectGoodsByType(string);
		if(a==null || a.size()==0)
			return ApiResult.fail("暂无商品");
		List<String> goodsIds = new ArrayList<String>();
		for (ClyjGoodsType clyjGoodsType : a) {
			goodsIds.add(clyjGoodsType.getGoodsId());
		}
		QueryWrapper<ClyjGoodsHomepage> w = new QueryWrapper<ClyjGoodsHomepage>();
		w.in("goods_id", goodsIds);
		return ApiResult.success(clyjGoodsHomepageMapper.selectList(w));
	}




}
